Welcome back.
In this lecture we're going to take a look at a concept called abstraction.
Abstraction is the idea of hiding unnecessary details from the user of a class.
Any functions or variables that exist in the class for the sole purpose of only working internally should
be hidden away from view, and should not be visible to the user.
As an example, think of a laptop and how its manufacturers allow you to interact with it using only
the keyboard and the trackpad, and they hide all the internal working components within a shroud or
cover around the laptop.
This is the same idea we should uphold for when we create classes and module scripts.
Any functions or variables that do not need to be accessed outside of the module script should be hidden
away.
So let's go ahead and get started scripting with an example.
I'm going to create a new class.
This class is going to be called rectangle.
And some basic properties that a rectangle can have is a position on the x axis, a position on the
y axis, and then maybe the width and the height of the rectangle.
And then from this point we can set the underscore underscore index meta method equal to the rectangle
itself.
And then let's create a constructor for the rectangle class.
Will pass the x, the y, the width and the height.
And then from here we'll create a new table.
From the set meta table function, attach the rectangle table to it, and then we can set all of the
properties accordingly.
And we'll make sure to return self back to where this function was called.
And from this point, maybe we want to have a function to calculate the area for a given rectangle.
So we can do rectangle.
Call this function get area.
And what we can do is return the area of this rectangle.
But let's say I want to calculate the area internally.
What we could do is create a function I'm going to call it calculate area.
Pass the rectangle we want to calculate the area for and then return the self dot width multiplied by
the self dot height.
And then we can call the function here.
And pass the rectangle.
So this function right here is hidden away inside of this module script.
Any script that requires this module script is not going to see this function.
We're upholding the concept of abstraction here.
We're hiding away the unnecessary details.
The only function we want to see is the get area function, which is just going to return to us a number.
But actually calculating the number in this local function, we don't want to see because we don't care
about it.
The only thing we care about is interacting with the class itself with these functions.
And then at the very end of our module script, we can return this rectangle table.
And then we can go into this other script that I have here.
I'm going to make a reference to server storage.
And then I'm going to access my rectangle class.
Which is in server storage.
I have a folder in there called classes.
And inside of there that is where my rectangle module script exists.
And then from this point I can create a new rectangle using the rectangle dot new function, give it
a position of zero and zero, and we can give it a width of ten and a height of 20.
And then from this point, what we could do is that we could get the area for our rectangle.
And we can store in any variable called area.
Now, as you can see, when I index my new rectangle, I'm only able to access the properties that exist
within this class.
Here I can only access the functions, and I can access the properties that exist within my rectangle.
However, I cannot access this function here.
I can't see it.
However, this would be different if I made this function a part of the rectangle class.
So if I did this instead, when I go to index my new rectangle, I can see my function appear right
here.
However, since it's working only internally within our module script, we don't want to be able to
see it here because it doesn't matter to us.
That's why we make this function only belong to the module script itself.
And it's local.
We're hiding it away.
And now I can print the area from our rectangle.
I can just print this rectangle has an area of and concatenate it with the area.
We can go and run our game and we should get an area of 200.
There we go.
This rectangle has an area of 200.
So the basic idea of abstraction is just to hide any unnecessary details.
We don't need to see when we're interacting with our class.
Hope you learned a lot and I'll see you in the next lecture.